﻿using System;
using System.Collections.Generic;
using System.Transactions;
using System.Messaging;
using System.ServiceModel;
using System.Runtime.Serialization;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.Factory;
using StockTrader.BusinessService.DataContract;
using StockTrader.BusinessService.Contract;
using StockTrader.BusinessService.Config;
using StockTrader.BusinessService.Utilities;


namespace StockTrader.BusinessService.Implementation
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
    public class TradeService
    {
/*
                private IOrder dalOrder;
        
                #region ITraderService Members
                public OrderData placeOrder(string OrderNo, string NextOrderId, int AccountID, int MemberId, string BrokerId, DateTime OpenDate, DateTime Gtd, DateTime CompletionDate, int OrderType, int OrderSide, int OrderClass, int OrderMode, int OrderStatus, int Priority, string Symbol, decimal Price, long Quantity, decimal OrderFee, long MatchQtty, long CancelQtty, long RemainQtty, decimal QuotePrice, string RejectReason, string Phone, string SessionNo, DateTime SettlementDate, int SettlementType, int HoldingId)
                {
                    OrderData order = null;
                    dalOrder = StockTrader.DAL.Factory.Order.Create(Settings.DAL);
                    System.Transactions.TransactionOptions txOps = new TransactionOptions();
                    txOps.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                    txOps.Timeout = TimeSpan.FromSeconds(Settings.SYSTEMDOTTRANSACTION_TIMEOUT);
                    //Start our System.Transactions tx with the options set above.
                    using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required, txOps))
                    {
                        dalOrder.Open(Settings.TRADEDB_SQL_CONN_STRING);
                        try
                        {
                            //Business Step 1:  create the order header.
                            order = createOrder(OrderNo, NextOrderId, AccountID, MemberId, BrokerId, OpenDate, Gtd, CompletionDate, OrderType, OrderSide, OrderClass, OrderMode, OrderStatus, Priority, Symbol, Price, Quantity, OrderFee, MatchQtty, CancelQtty, RemainQtty, QuotePrice, RejectReason, Phone, SessionNo, SettlementDate, SettlementType, HoldingId);
        
                            tx.Complete();
                            return order;
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            dalOrder.Close();
                        }
                    }
                }
        
                public OrderData placeOrderOverSMS(int AccountID, int OrderType, int OrderSide, string Symbol, decimal Price, long Quantity)
                {
                    OrderData order = null;
                    dalOrder = StockTrader.DAL.Factory.Order.Create(Settings.DAL);
                    System.Transactions.TransactionOptions txOps = new TransactionOptions();
                    txOps.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                    txOps.Timeout = TimeSpan.FromSeconds(Settings.SYSTEMDOTTRANSACTION_TIMEOUT);
                    using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required, txOps))
                    {
                        dalOrder.Open(Settings.TRADEDB_SQL_CONN_STRING);
                        try
                        {
                            order = createOrder("", "", AccountID, 0, "", DateTime.Now, DateTime.Now, DateTime.Now, OrderType, OrderSide, 9, 1, 0, 0, Symbol, Price, Quantity, 0, 0, 0, 0, 0, "", "", "", DateTime.Now, 1, 0);
        
                            tx.Complete();
                            return order;
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            dalOrder.Close();
                        }
                    }  
                }
        
                #endregion
        
                private OrderData createOrder(string OrderNo, string NextOrderId, int AccountID, int MemberId, string BrokerId, DateTime OpenDate, DateTime Gtd, DateTime CompletionDate, int OrderType, int OrderSide, int OrderClass, int OrderMode, int OrderStatus, int Priority, string Symbol, decimal Price, long Quantity, decimal OrderFee, long MatchQtty, long CancelQtty, long RemainQtty, decimal QuotePrice, string RejectReason, string Phone, string SessionNo, DateTime SettlementDate, int SettlementType, int HoldingId)
                {
                    OrderData order;
                    switch (OrderType)
                    {
                        case Util.ORDER_TYPE_BUY:
                            order = dalOrder.CreateOrder(OrderNo, NextOrderId, AccountID, MemberId, BrokerId, DateTime.Now, Gtd, CompletionDate, OrderType, Util.ORDER_TYPE_BUY, OrderClass, OrderMode, OrderStatus, Priority, Symbol, Price, Quantity, OrderFee, MatchQtty, CancelQtty, RemainQtty, QuotePrice, RejectReason, Phone, SessionNo, SettlementDate, SettlementType, HoldingId);
                            break;
                        case Util.ORDER_TYPE_SELL:
                            order = dalOrder.CreateOrder(OrderNo, NextOrderId, AccountID, MemberId, BrokerId, DateTime.Now, Gtd, CompletionDate, OrderType, Util.ORDER_TYPE_SELL, OrderClass, OrderMode, OrderStatus, Priority, Symbol, Price, Quantity, OrderFee, MatchQtty, CancelQtty, RemainQtty, QuotePrice, RejectReason, Phone, SessionNo, SettlementDate, SettlementType, HoldingId);
                            break;
                        default:
                            throw new Exception(Util.EXCEPTION_MESSAGE_BADORDERTYPE);
                    }
                    return order;
                }*/
        


    }
}
